package com.gin.kotlin.no_gay_alliance.adapter.pm

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.pm.PrivateMessageReplyActivity
import com.gin.kotlin.no_gay_alliance.argument.PrivateMessageReplyArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemPrivateMessageBinding
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils.Companion.zdtIntoTextView
import com.gin.nga.enums.PriMessageStatus
import com.gin.nga.response.message.PriMessage
import com.gin.nga.utils.MathUtils
import java.util.Locale

class PrivateMessageAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemPrivateMessageBinding, PriMessage>(context, handler),
    View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemPrivateMessageBinding>, position: Int, b: ItemPrivateMessageBinding, dataModel: PriMessage) {
        b.tvTitle.text = dataModel.title
        b.tvFromUser.text = dataModel.fromUsername ?: "#系统账号#"
        b.tvLastUser.text = dataModel.lastReplyUsername
        b.tvPostCount.text = "(${dataModel.postCount})"

        val defaultTextColor = getAttrColor(R.attr.textColor)
        // 创建时间文字，颜色
        dataModel.timeCreated?.also { zdtIntoTextView(context, it, b.tvTimeCreated, defaultTextColor) }
        // 最后回复时间文字，颜色
        dataModel.lastModify?.also { zdtIntoTextView(context, it, b.tvLastModify, defaultTextColor) }
        // 状态标签
        b.tvStatus.text = SpannableStringBuilder().also { builder ->
            val status = dataModel.getStatus()
            if (status?.contains(PriMessageStatus.FROM_SYSTEM) == true) {
                val text = String.format(Locale.CHINESE, "[%s]", PriMessageStatus.FROM_SYSTEM.tag)
                builder.append(text)
                val span = ForegroundColorSpan(getAttrColor(R.attr.textColorRed))
                builder.setSpan(span, builder.length - text.length, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            if (status?.contains(PriMessageStatus.UNREAD) == true) {
                val text = String.format(Locale.CHINESE, "[%s]", PriMessageStatus.UNREAD.tag)
                builder.append(text)
                val span = ForegroundColorSpan(getAttrColor(R.attr.textColorRed))
                builder.setSpan(span, builder.length - text.length, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            if (status?.contains(PriMessageStatus.MULTI) == true) {
                val text = String.format(Locale.CHINESE, "[%s]", PriMessageStatus.MULTI.tag)
                builder.append(text)
                val span = ForegroundColorSpan(getAttrColor(R.attr.textColorBlue))
                builder.setSpan(span, builder.length - text.length, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
        }
        b.tvStatus.visibility = if (b.tvStatus.text.isEmpty()) View.GONE else View.VISIBLE

        b.root.tag = dataModel
        b.root.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        val dataModel = v?.tag as? PriMessage ?: return
        PrivateMessageReplyActivity.start(
            context, PrivateMessageReplyArgument(
                dataModel.id!!,
                totalPage = MathUtils.getTotalPage(dataModel.postCount, 20),
                title = dataModel.title,
                fromSystem = dataModel.fromUserId == null || dataModel.fromUserId == 0L
            )
        )
    }
}